package org.lcc.Service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.lcc.Service.QuestionService;
import org.lcc.mapper.QuestionMapper;
import org.lcc.model.VO.QuestionAllVo;
import org.lcc.model.VO.SearchSet;
import org.lcc.model.entity.BusinessException;
import org.lcc.model.entity.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionServicelmpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    /**
     * Jackson库中的ObjectMapper实例，用于JSON与Java对象的相互转换
     * ObjectMapper提供了用于序列化和反序列化的功能，是处理JSON数据时不可或缺的工具
     */
    private ObjectMapper objectMapper;

    @Override
    @Transactional
    public boolean batchInsertion(List<Question> questions) {
        for (Question question : questions) {
            try {
                String optionsJson = objectMapper.writeValueAsString(question.getOptions());
                question.setOptionsJson(optionsJson);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("序列化选项失败", e);
            }
        }
        questionMapper.batchInsertQuestions(questions);
        return true;
    }

    //单独创建试题
    @Override
    public Boolean importQuestion(Question question) {
        try {
            String optionsJson = objectMapper.writeValueAsString(question.getOptions());
            question.setOptionsJson(optionsJson);
            questionMapper.insertQuestion(question);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(400, "试题插入失败");
        }
    }

    //回显试题详情
    @Override
    public Question selectQuestion(Integer questionId) {
        if (questionId != null) {
            Question question = questionMapper.selectQuestion(questionId);
            System.out.println(question);
            if (question != null) {
                try {
                    List<String> options = objectMapper.readValue(question.getOptionsJson(), List.class);
                    question.setOptions(options);
                    question.setOptionsJson(null);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BusinessException(400, "试题详情获取失败");
                }
                return question;
            }
        }
        return null;
    }

    //修改试题
    @Override
    public Boolean updateQuestion(Question question, Integer questionId) {
        try {
            String optionsJson = objectMapper.writeValueAsString(question.getOptions());
            question.setOptionsJson(optionsJson);
            questionMapper.updateQuestion(question, questionId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(400, "试题修改失败");
        }
    }

    //批量/单独删除试题
    @Override
    public Boolean deleteQuestions(List<Integer> questionIds) {
        if (questionIds == null || questionIds.isEmpty()) {
            return false;
        }
        try {
            int deletedCount = questionMapper.deleteQuestions(questionIds);
            return deletedCount == questionIds.size();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //根据关键字进行模糊查询
    @Override
    public SearchSet searchQuestion(String questionName, String type, String difficulty) {
        List<Question> questions = questionMapper.searchQuestion(questionName, type, difficulty);
        if (questions != null) {
            for (Question question : questions) {
                try {
                    List<String> options = objectMapper.readValue(question.getOptionsJson(), List.class);
                    question.setOptions(options);
                    question.setOptionsJson(null);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new BusinessException(400, "试题详情获取失败");
                }
            }
        }
        return new SearchSet(questions, questions.size());
    }

    //获取题库里面的试题
    @Override
    public PageInfo<QuestionAllVo> selectQuestionAll(String questionName,
                                                     String type,
                                                     String difficulty,
                                                     String subject,
                                                     String keyword,
                                                     Integer page,
                                                     Integer size) {
        // 设置分页
        PageHelper.startPage(page, size);

        // 查询数据
        List<Question> questions = questionMapper.selectQuestionAll(questionName, type, difficulty, subject, keyword);

        // 转换为 VO 对象
        List<QuestionAllVo> questionAllVos = questions.stream().map(question -> {
            QuestionAllVo vo = new QuestionAllVo();
            vo.setQuestionId(question.getQuestionId());
            vo.setType(question.getType());
            vo.setContent(question.getContent());
            vo.setAnswer(question.getAnswer());
            vo.setSubject(question.getSubject());
            vo.setDifficulty(question.getDifficulty());
            vo.setUpdateTime(question.getUpdateTime());
            return vo;
        }).collect(Collectors.toList());

        // 创建 PageInfo 对象
        return new PageInfo<>(questionAllVos);
    }
}
